package com.weiyi.mashangzhu.Landlord.Adapter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.Landlord.Bean.PhotoBean;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Utils.GlideImgManager;
import com.weiyi.mashangzhu.Utils.T;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by win7 on 2017/10/27.
 */

public class GridAdapter_Photo extends ArrayAdapter<PhotoBean> {
    private Context mContext;
    private int layoutResourceId;
    private List<PhotoBean> mGridData = new ArrayList<PhotoBean>();
    String info;


    public GridAdapter_Photo(Context context, int resource, List<PhotoBean> objects, String info) {
        super(context, resource, objects);
        this.mContext = context;
        this.layoutResourceId = resource;
        this.mGridData = objects;
        this.info = info;
    }

    public void setGridData(ArrayList<PhotoBean> mGridData) {
        this.mGridData = mGridData;
        notifyDataSetChanged();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {
            LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
            convertView = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.img = (ImageView) convertView.findViewById(R.id.img);
            holder.close = (ImageView) convertView.findViewById(R.id.close);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        // 加载本地图片
        if (mGridData.get(position).getInfo().equals("post")) {
            File file = new File(mGridData.get(position).getUrl());
            Glide.with(mContext).load(file).into(holder.img);
            if (info.equals("edit")) {
                holder.close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        int post_id = -1;
                        for (int i = 0; i < MyApp.post_url.size(); i++) {
                            if (mGridData.get(position).getUrl().equals(MyApp.post_url.get(i).getUrl())) {
                                post_id = i;
                            }
                        }
                        if (post_id != -1) {

                            MyApp.post_url.remove(post_id);
                        }

                        mGridData.remove(position);
                        notifyDataSetChanged();
                    }
                });
            } else {
                holder.close.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        MyApp.images.remove(position + 1);
                        mGridData.remove(position);
                        notifyDataSetChanged();
                    }
                });
            }

        } else if (mGridData.get(position).getInfo().equals("get")) {
            GlideImgManager.glideLoader(mContext, API.BaseApi_url + mGridData.get(position).getUrl(), R.mipmap.errer, holder.img);
            holder.close.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (position <= MyApp.get_url.size() + 1) {
                        MyApp.get_url.remove(position + 1);
                    }
                    MyApp.img_url.remove(position + 1);
                    mGridData.remove(position);
                    notifyDataSetChanged();
                }
            });
        }


        return convertView;
    }

    private class ViewHolder {
        ImageView img, close;

    }


}
